#include "argamelogic.h"

#include <cv.h>
#include <highgui.h>
#include "glut.h"

#include "trackingsystem.h"
#include "videodevicemanager.h"


ARGameLogic::ARGameLogic(int _camWidth, int _camHeight)
	: GameLogic(),
	mCamWidth(_camWidth),
	mCamHeight(_camHeight)
{
	Init();
}

ARGameLogic::~ARGameLogic()
{
	if (mTrackingSystem)
	{
		delete mTrackingSystem;
		mTrackingSystem = 0;
	}

	if (mVideoDeviceManager)
	{
		delete mVideoDeviceManager;
		mVideoDeviceManager = 0;
	}
}


void ARGameLogic::Init()
{
	InitTracking();
}

void ARGameLogic::Update(float _elapseTime)
{
	for (int videoDeviceInd = 0; videoDeviceInd < mVideoDeviceList.size(); ++videoDeviceInd)
	{
		if (mVideoDeviceList[videoDeviceInd]->Update())
		{
			mTrackingSystem->Update(const_cast<unsigned char*>(mVideoDeviceList[videoDeviceInd]->GetBufferData()));
		}
	}
}


void ARGameLogic::Render()
{

}

void ARGameLogic::InitTracking()
{
	mVideoDeviceManager = new VideoDeviceManager();
	mTrackingSystem = new TrackingSystem();
	mTrackingSystem->Init(mCamWidth, mCamHeight);

	mVideoDeviceList.push_back((*mVideoDeviceManager)[0]);
	for(int i = 0; i < mVideoDeviceList.size(); ++i)
	{
		mVideoDeviceList[i]->Init(mCamWidth, mCamHeight);
	}
}